const express = require("express");
const cors = require("cors");
const fileUpload = require("express-fileupload");
const expressWs = require("express-ws");
const logger = require("morgan");
const path = require("path");
const process = require("process");
const dotenv = require("dotenv");
const verifyStatus = require("./middleware/verifyStatus.js");
const websocketRouter = require("./routes/websocket.js");
const trainRouter = require("./routes/train.js");
const testRouter = require("./routes/test.js");
const cvmodelRouter = require("./routes/cvmodel.js");
const datasetRouter = require("./routes/dataset.js");

dotenv.config();
const app = express();
app.use(logger("dev"));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(fileUpload());
app.use(cors());
app.use(express.static(path.join(__dirname, "static")));
expressWs(app);

app.ws("/websocket", websocketRouter);
app.use("/train", trainRouter);
app.use("/test", testRouter);
app.use("/cvmodel", cvmodelRouter);
app.use("/dataset", datasetRouter);
app.use("/status", (req, res) => {
  res.json({ code: 0, status: req.app.locals.status });
});

app.locals.status = "WAITING";

const port = process.env.APP_PORT;
app.listen(port, () =>
  console.log(`Listening on port http://localhost:${port}`)
);
